package com.sky.demo02;

/**
 * 目标：演示可见性问题
 * 1. 创建一个共享变量
 * 2. 创建一条线程不断读取共享变量
 * 3. 创建一条线程修改共享变量
 */
public class Test01Visibility {

    private static boolean flag = true;

    private static final Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            while (flag){
                // Synchronized会对应lock原子操作，会刷新工作内存中共享变量的值
                synchronized (lock) {

                }
            }
        });
        t1.start();

        Thread.sleep(2000);

        Thread t2 = new Thread(() -> {
            flag = false;
            System.out.println("线程修改了变量的值为false。");
        });
        t2.start();

    }
}
